package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.concurrent.FutureTask;

public interface IEventHandler<S extends EventArgs> extends IDelegate {

    void invoke(@Nullable Object sender, @Nullable S data) throws Exception;

    default FutureTask<Void> invokeAsync(@Nullable Object sender, @Nullable S data, @Nullable Callback callback) {
        FutureTask<Void> futureTask = new FutureTask<>(() -> {
            invoke(sender, data);
            if (callback != null) {
                callback.invoke(new InvokeData(sender, new Object[]{data}, null));
            }
            return null;
        });

        ASYNC_DELEGATE_THREAD_POOL.submit(futureTask);
        return futureTask;
    }
}
